package com.walkline.dianping.dao;

import org.json.me.JSONArray;
import org.json.me.JSONException;
import org.json.me.JSONObject;

import com.walkline.dianping.DianpingException;
import com.walkline.dianping.DianpingSDK;
import com.walkline.dianping.inf.DealIdList;

public class DianpingDealIdList extends DianpingObject implements DealIdList
{
	private int _count = 0;
	private String[] _id_list;

	public DianpingDealIdList(DianpingSDK dianping, JSONObject jsonObject) throws DianpingException
	{
		super(dianping, jsonObject);

		_count = _jsonObject.optInt("count");

		JSONArray listArray = _jsonObject.optJSONArray("id_list");

		if (listArray != null)
		{
			_id_list = new String[listArray.length()];

			for (int i=0; i<listArray.length(); i++)
			{
				try {
					_id_list[i] = (String) listArray.get(i);
				} catch (JSONException e) {}
			}
		}
	}

	//本次API访问状态，如果成功返回"OK"，并返回结果字段，如果失败返回"ERROR"，并返回错误说明
	public String getStatus() {return _jsonObject.optString("status");}

	//本次API访问所获取的单页团购ID数量
	public int getCounts() {return _count;}

	//团购单ID列表
	public String[] getIDList() {return _id_list;}

	//指定数量的团购单ID列表
	public String[] getIDList(int count)
	{
		if (count >= _count) {return _id_list;}

		String[] newList = new String[count];

		for (int i=0; i<count; i++)
		{
			newList[i] = _id_list[i];
		}

		return newList;
	}
}